//
//  ImageUtil.m
//  SIM
//
//  Created by 奕 朱 on 11-3-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#define ExceptionInfo ISOExceptionInfo
#define ColorInfo ISOColorInfo


#import "MagickWand.h"
#import "ImageUtil.h"

@implementation ImageUtil

+ (void) resizeFromPath: (NSString *) fromPath toPath: (NSString *) toPath {
	
		
	//	// printout ImageMagick version
		NSLog(@"%s", GetMagickVersion(nil));
	
	#define ThrowWandException(wand) \
	{ \
	char \
	*description; \
	\
	ExceptionType \
	severity; \
	\
	description=MagickGetException(wand,&severity); \
	(void) fprintf(stderr,"%s %s %u %s\n",GetMagickModule(),description); \
	description=(char *) MagickRelinquishMemory(description); \
	exit(-1); \
	}
		
		MagickWandGenesis();
		MagickWand * magick_wand= NewMagickWand();
	
		NSString * path = [[NSBundle mainBundle] resourcePath];
		
		/*
		 Read Image
		 */
		NSString *picpath= fromPath;
		MagickBooleanType status = MagickReadImage(magick_wand,[picpath UTF8String]);
		
		if (status == MagickFalse) {
			//ThrowWandException(magick_wand);
			return 0;
		}
		
		/*
		 Turn the images into a thumbnail sequence.
		 */
		MagickResetIterator(magick_wand);
		while (MagickNextImage(magick_wand) != MagickFalse){
			MagickResizeImage(magick_wand,106,80,LanczosFilter,1.0);
		}
		
		/*
		 Write the image then destroy it.
		 */
		NSString *outpath= toPath;
		status=MagickWriteImages(magick_wand,[outpath UTF8String],MagickTrue);
		if (status == MagickFalse)
			//ThrowWandException(magick_wand);
		magick_wand=DestroyMagickWand(magick_wand);
		MagickWandTerminus();
	
	
}

@end
